import re
import api, appModuleHandler, tones, ui, winUser
from controlTypes.role import Role
# 应用模块
class AppModule(appModuleHandler.AppModule):
    # 对话框处理
    def event_alert(self, obj, nextHandler):
        # 获取 obj 的子孙
        children = obj.recursiveDescendants
        # 如果没有子孙，就跳过
        if not children:
            nextHandler()
            return
        # 朗读对话框的内容
        items = set()
        for child in children:
            if child.role == Role.EDITABLETEXT:
                name = child.name
                if name:
                    items.add(re.sub(r'[a-z ]+\{(?:.+?;)+\}', '', name, re.I))
        ui.message('\n'.join(items))
    def event_foreground(self, obj, nextHandler):
        ws = obj.windowStyle
        if not (ws & winUser.WS_EX_APPWINDOW or ws & winUser.WS_GROUP):
            self.event_alert(obj, nextHandler)
            return
        nextHandler()
    def _get_statusBar(self):
        # 获取状态栏的方法
        if hasattr(self, 'statusBarObject'):
            return self.statusBarObject
        obj = api.getForegroundObject()
        for child in obj.children:
            for child2 in child.children:
                for child3 in child2.children:
                    for child4 in child3.children:
                        for child5 in child4.children:
                            for child6 in child5.children:
                                for child7 in child6.children:
                                    for child8 in child7.children:
                                        if child8.role == Role.STATUSBAR:
                                            self.statusBarObject = child8
                                            return child8